<?

//Initiate Authenticate
SecurityRedirect ();

//initiate Vars
//define("PRIMARY_KEY","kedept");
//define("PRIMARY_KEY","id");
define("PRIMARY_KEY","unit_code");
$FileName        = "ais_setup_unit.php";
$FileTemplate    = "ais_setup_unit_form.html";
$SubHeaderTitle  = "Setup Unit";
$HTMLFormName    = "UnitForm";
$TableName       = "tblunit";
$FormAction      = $FileName . "?" . AddParam(GetQueryString("QueryString", ""),"form","$HTMLFormName");
$FormEncType     = "application/x-www-form-urlencoded";
//$PMode         = "";

//Initiate Database Table For Insert & Update;
$FormFields    = array("unit_code","unit_name");
buildsqlfield ($TableName, $FormFields);

//Leave Here Untouch
$FormCName     = BuildHTMLField($FormFields,"name");
$FormCValue    = BuildHTMLField($FormFields,"value");

//************ FORM ACTION **************
if (GetParam("form","")==$HTMLFormName) {
    if (GetParam("Action","")=="Add") {
        $FormMode="Edit";
        FormInsert();
    }
    if (GetParam("Action","")=="Update") {
        $FormMode="Edit";
        FormUpdate();
    }
    if (GetParam("Action","")=="Delete") {
        $FormMode="Edit";
        FormDelete();
    }
    if (GetParam("Action","")=="Cancel") {
        $FormMode="Edit";
        FormCancel();
    }
}

//Field Verification
$Verification  = "Enable";
$SessionVar    = md5(microtime());
//$SessionVar    = "Stuck";
$VerifyFields  = array("unit_code"=>"Department Code",
                       "unit_name"=>"Department Name"
                       );
VerifyHTML ($HTMLFormName,$VerifyFields,$SessionVar);

//************ SET FORM MODE ***************

if (GetParam(PRIMARY_KEY,"")) $FormMode = "Edit";
else $FormMode = "Add";

Init($SessionVar,CreateVerifyLoader ($FileName,"iLoader",PRIMARY_KEY,"$HTMLFormName.".PRIMARY_KEY, $FormMode));
$InitFunction = "isExist".PRIMARY_KEY."()";

//******* DATABASE FORM QUERY ***********
$SQL = "SELECT * FROM `$TableName` ";
if (GetParam(PRIMARY_KEY,"")) {
    $SQLForm = $SQL."WHERE ".PRIMARY_KEY."='".GetParam(PRIMARY_KEY,"")."'";
}
//******* SHOW FORM CONTENT *********
$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate($MOD_TEMPLATE_DIR."/".$FileTemplate);

if (GetParam(PRIMARY_KEY,"")) $TBS->MergeBlock("blv","mysql",$SQLForm);
else $TBS->MergeBlock("blv",$FormCValue);
//HTML Name
$TBS->MergeBlock("bln",$FormCName);
//HTMLGrid
$TBS->MergeBlock("blg",$DBMysql,$SQL);
$TBS->Show();


//*************** ACTION FUNCTION *******************
function FormInsert() {
    global $DBConnection,$FileName;
    $DBConnection->Insert();
    header("Location: $FileName?PageMode=Form");
    exit;
}

function FormUpdate() {
    global $DBConnection;
    $DBConnection->Update();
}

function FormDelete() {
    global $DBConnection,$FileName,$TableName;
    $DBConnection->Delete("$TableName",PRIMARY_KEY);
    header("Location: $FileName");
    exit;

}

function FormSearch () {

}

function FormCancel () {
    global $FileName;
    header("Location: ".$FileName);
    exit;
}
//*************** USER FUNCTION *******************
function buildsqlfield ($table, $arrayfield) {
        global $DBConnection;
        for ($x=0;$x<count($arrayfield);$x++) {
            $DBConnection->add_htmlfield($table,$arrayfield[$x],GetParam($arrayfield[$x],""));
        }
}

?>
